﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using Microsoft.AspNetCore.Http;

using KSOAdmin.Models.ViewModel;

using Microsoft.EntityFrameworkCore.Storage;
using KSOAdmin.Core.EFDbContext;
using Microsoft.EntityFrameworkCore;
using KSOAdmin.Models.DomainModels.System;
using System.Threading.Tasks;

namespace KSOAdmin.Core.Other
{
    public static class UserObtain
    {

        public static int UserId
        {
            get
            {
                Claim claim = KSOHttpContext.Current?.User.Claims.Where(w => w.Type == "sid").FirstOrDefault();
                return claim == null ? 0 : claim.Value.ObjToInt();
            }
        }

        public static View_Sys_User GetUserInfo(int UserId)
        {
            using (KSOContext kSO = new KSOContext())
            {
                View_Sys_User User = kSO.Set<Sys_User>().AsNoTracking().Where(w => w.ID == UserId).Select
                    (w => new View_Sys_User { ID = w.ID, UserName = w.UserName, Role_Id = w.Role_Id, Email = w.Email, PhoneNo = w.PhoneNo, Dept_Id = w.Dept_Id, Address = w.Address }).FirstOrDefault();
                if (User is null)  return null;

                _userInfo = User;
                return User;
            }
        }

        public static View_Sys_User View_Sys_User
        {
            get
            {
                if (_userInfo != null)
                {
                    return _userInfo;
                }
                return GetUserInfo(UserId);
            }
        }

        private static View_Sys_User _userInfo { get; set; }


        public static int Role_Id
        {
            get { return View_Sys_User.Role_Id; }
        }

        public static string PhoneNo
        {
            get { return View_Sys_User.PhoneNo; }
        }

        public static string UserName
        {
            get { return View_Sys_User.UserName; }
        }

        /// <summary>
        /// 角色ID为1的默认为超级管理员
        /// </summary>
        public static bool IsSuperAdmin
        {
            get { return Role_Id == 1; }
        }


    }
}
